﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace echuCMS
{
    public partial class FormSalesMan : Form
    {
        private BindingSource bsSalesMan = new BindingSource();
        private BindingSource bsCity = new BindingSource();
        public FormSalesMan()
        {
            InitializeComponent();
        }

        private void FormSalesMan_Shown(object sender, EventArgs e)
        {
            //查询所有已经存在的业务员
            getAllSalesMan();
            //加载城市
            getCity();
        }

        /// <summary>
        /// 加载城市
        /// </summary>
        private void getCity()
        {
         List<City> cityList = SalesManService.getCity();
            cityList.Insert(0,new City() {id = 0,city = "请选择" });
            bsCity.DataSource = cityList;
            this.combCity.DataSource = bsCity;
            this.combCity.DisplayMember = "CityName";
            this.combCity.ValueMember = "CityID";
        }

        /// <summary>
        /// 查询所有已经存在的业务员
        /// </summary>
        private void getAllSalesMan()
        {
            bsSalesMan.DataSource = SalesManService.getAllSalesMan();
            dataGridViewSalesMan.DataSource = bsSalesMan;
        }

        private void FormSalesMan_Load(object sender, EventArgs e)
        {
            buttonDelete.Enabled = false;
            buttonEdit.Enabled = false;
            dataGridViewSalesMan.AutoGenerateColumns = false;
        }

        private void dataGridViewSalesMan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (bsSalesMan.DataSource != null)
                {
                    SalesMan salesMan = bsSalesMan.Current as SalesMan;
                    this.textBoxSalesCode.Text = salesMan.SalesManCode;
                    textBoxSalesName.Text = salesMan.SalesManName;
                    textBoxSalesPhone.Text = salesMan.SalesManPhone;
                    buttonDelete.Enabled = true;
                    buttonEdit.Enabled = true;
                    buttonAdd.Enabled = false;
                    foreach (var item in combCity.Items)
                    {
                        if ((item as City).id == salesMan.SalesManCityID)
                        {
                            combCity.SelectedItem = item;
                        }
                    }
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            SalesMan salesMan = new SalesMan();
            if (string.IsNullOrEmpty(textBoxSalesCode.Text.Trim()))
            {
                MessageBox.Show("业务员编号不可为空");
                return;
            }
            else
            {
                salesMan.SalesManCode = textBoxSalesCode.Text.Trim();
            }

            if (string.IsNullOrEmpty(textBoxSalesName.Text.Trim()))
            {
                MessageBox.Show("业务员编号不可为空");
                return;
            }
            else
            {
                salesMan.SalesManName = textBoxSalesName.Text.Trim();
            }

            if (string.IsNullOrEmpty(textBoxSalesPhone.Text.Trim()))
            {
                MessageBox.Show("业务员编号不可为空");
                return;
            }
            else
            {
                salesMan.SalesManPhone = textBoxSalesPhone.Text.Trim();
            }

            if (combCity.SelectedValue.ToString() == "0")
            {
                MessageBox.Show("请选择业务员所在城市");
                return;
            }
            else
            {
                salesMan.SalesManCityID = long.Parse(combCity.SelectedValue.ToString());
            }

           int r = SalesManService.insertSalesMan(salesMan);
            if (r > 0)
            {
                getAllSalesMan();
            }
            else
            {
                MessageBox.Show("添加失败，请联系开发者");
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (bsSalesMan.Current != null)
            {
                SalesMan salesMan = bsSalesMan.Current as SalesMan;
                if (string.IsNullOrEmpty(textBoxSalesCode.Text.Trim()))
                {
                    MessageBox.Show("业务员编号不可为空");
                    return;
                }
                else
                {
                    salesMan.SalesManCode = textBoxSalesCode.Text.Trim();
                }
                if (string.IsNullOrEmpty(textBoxSalesName.Text.Trim()))
                {
                    MessageBox.Show("业务员编号不可为空");
                    return;
                }
                else
                {
                    salesMan.SalesManName = textBoxSalesName.Text.Trim();
                }
                if (string.IsNullOrEmpty(textBoxSalesPhone.Text.Trim()))
                {
                    MessageBox.Show("业务员编号不可为空");
                    return;
                }
                else
                {
                    salesMan.SalesManPhone = textBoxSalesPhone.Text.Trim();
                }

                if (combCity.SelectedValue.ToString() == "0")
                {
                    MessageBox.Show("请选择业务员所在城市");
                    return;
                }
                else
                {
                    salesMan.SalesManCityID = long.Parse(combCity.SelectedValue.ToString());
                }

                int r = SalesManService.updateSalesMan(salesMan);
                if (r > 0)
                {
                    MessageBox.Show("修改成功");
                    getAllSalesMan();
                }
                else
                {
                    MessageBox.Show("修改失败，请联系开发者");
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (bsSalesMan.Current != null)
            {
                int i = SalesManService.deleteSalesMan((bsSalesMan.Current as SalesMan));
                if (i > 0)
                {
                    getAllSalesMan();
                }
                else
                {
                    MessageBox.Show("删除失败，请联系开发者");
                }
            }
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            buttonDelete.Enabled = false;
            buttonEdit.Enabled = false;
            buttonAdd.Enabled = true;
            textBoxSalesCode.Text = "";
            textBoxSalesName.Text = "";
            textBoxSalesPhone.Text = "";
        }

        private void dataGridViewSalesMan_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.Value is City)
            {
                e.Value = (e.Value as City).city;
            }
        }
    }
}
